//
//  ZYBaseViewController.swift
//  SwiftDemo1
//
//  Created by wzp on 2025/6/1.
//

import Foundation
import UIKit



class ZYBaseViewController: UITableViewController {
    var isLogin: Bool {
        return ZYUserCore.shareCore.isLogin
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    lazy private(set) var visitorView: ZYVisitorView = {
        let visiotrView = ZYVisitorView()
        return visiotrView
    }()
    
    override func loadView() {
        isLogin ? super.loadView() : loadVisitorView()
    }
    
    private func loadVisitorView() {
        self.view = visitorView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !isLogin {
            updateVisitorView()
        }
        setupNavigationBar()
        
        NotificationCenter.default.addObserver(self, selector: #selector(reloadView), name: .init(VcReloadViewNote), object: nil)
    }
    func updateVisitorView() {}
    
    func setupNavigationBar() {
        if !isLogin {
            navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(clickRegisterBtn))
            navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(clickLoginBtn))
        }
    }
}

// MARK: -nativagtionItem click event
extension ZYBaseViewController {
    @objc private func clickRegisterBtn() {
        
    }
    
    @objc private func clickLoginBtn() {
        let authVc = ZYAuthViewController()
        let nvc = UINavigationController(rootViewController: authVc)
        nvc.modalPresentationStyle = .fullScreen
        present(nvc, animated: true)
    }
}

extension ZYBaseViewController {
    @objc private func reloadView() {
        self.view.subviews.forEach({ $0.removeFromSuperview() })
        self.view = nil
        self.loadViewIfNeeded()
    }
}
